package com.dxb.test.thread;

/**
 * Description：三个线程顺序执行 <br>
 * author：戴晓波 <br>
 * date：2022/2/14/014 10:07 <br>
 */
public class ABC {
    private Object lock = new Object();
    private volatile int num = 0;

    private void print(int target) {
        for (int i = 0; i < 10; i++) {
            synchronized (lock) {
                while (num % 3 != target) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(target);
                num++;
                lock.notifyAll();
            }
        }
    }

    public static void main(String[] args) {
        ABC abc = new ABC();
        new Thread(() -> {
            abc.print(0);
        }).start();
        new Thread(() -> {
            abc.print(1);
        }).start();
        new Thread(() -> {
            abc.print(2);
        }).start();
    }
}
